package cn.hesion.leetcode;
/**
 * ClassName: MapTimeMap <br/>
 * Description: <br/>
 * date: 2022/6/9 6:03<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.*;

/**
 *
 * @author: hesion
 * @create: 2022-06-09 06:03
 **/
public class MapTimeMap {
    Map<String, TreeMap<Integer,String>> map;
    public MapTimeMap() {
        map = new HashMap<String,TreeMap<Integer,String>>();
    }

    public void set(String key, String value, int timestamp) {
        map.computeIfAbsent(key, k-> new TreeMap()).put(timestamp, value);
    }

    public String get(String key, int timestamp) {
        TreeMap<Integer,String> inMap;
        inMap = map.get(key);
        if(inMap != null){
            SortedMap<Integer, String> integerStringSortedMap = inMap.headMap(timestamp+1);
            if(integerStringSortedMap.get(timestamp) !=null){
                System.out.println(integerStringSortedMap);
                Integer lastKey = integerStringSortedMap.lastKey();
                String s = integerStringSortedMap.get(lastKey);
                return s;
            }else {
                return "";
            }
        }else{
            return "";
        }
    }


}
